$(function() {
	admin.rule.init();
});
Namespace('admin.rule',{
	init:function() {
		easyui.util.initDatagrid('#datagrid',{
			onDblClickRow:function() {
				//system.user.update();
			}
		});
	},
	query:function() {
		if(!$('#query-form').form('validate')) return;
		var data=$('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load',data);
	},
	update:function(obj) {
		var id = $(obj).attr('id');
		top.addTab("2",'积分制度修改', contextPath+'/admin/rule/toEdit/'+id);
	},
	doUpdate:function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		if(data.rice){
			var rice = data.rice;
			if(data.rice.startsWith('-')){
				rice = data.rice.substring(1,data.rice.length);;
			}
			if(!rice.isNumber()){
				top.showInfo("贡献（米粒）只能填整数");
				return;
			}
		}
		if(data.trust){
			var trust = data.trust;
			if(data.trust.startsWith('-')){
				trust = data.trust.substring(1,data.trust.length);;
			}
			if(!trust.isNumber()){
				top.showInfo("信用分只能填整数");
				return;
			}
		}
		if(data.commission){
			var commission = data.commission;
			if(data.commission.startsWith('-')){
				commission = data.commission.substring(1,data.commission.length);
			}
			if(!commission.isNumber()){
				top.showInfo("提成只能填整数");
				return;
			}
		}
		$('#datagrid').datagrid('loading');
		$.postJson(contextPath+'/admin/rule/edit',data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==0) {
				$.messager.confirm('信息提示','修改成功!',function(success){
				    if(!success) return;
				    top.addTab('','积分制度列表', contextPath+'/admin/rule/list');
				});
			}else{
				top.showInfo(result.msg);
			}
		});
	},
	add:function() {
		top.addTab("1",'添加积分制度', contextPath+'/admin/rule/add');
	},
	doAdd:function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		var msg = [];
		if (data.name == null || data.name == '') {
			msg.push('行为名称不能为空');
		}
		if (data.cateChar == null || data.cateChar == '') {
			msg.push('行为类型不能为空');
		} else {
			var ok = /^[a-zA-Z]{1}$/.test(data.cateChar);
			if (!ok) {
				msg.push('行为类型只能是1位字母');
			}
		}
		if (data.cateNum == null || data.cateNum == '') {
			msg.push('行为编号不能为空');
		} else {
			var ok = /^[0-9]+$/.test(data.cateNum);
			if (!ok) {
				msg.push('行为编号只能是数字');
			}
		}
		if (msg.length > 0) {
			top.showInfo(msg.join(','));
			return;
		}
		$('#datagrid').datagrid('loading');
		$.postJson(contextPath+'/admin/rule/doAdd',data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==0) {
				$('#saveTexts').attr('disabled','disabled');
				$.messager.confirm('信息提示','保存成功!',function(success){
				    if(!success) return;
				    top.addTab('','积分制度列表', contextPath+'/admin/rule/list');
				});
			}else{
				top.showInfo('积分制度保存失败!');
			}
		});
	},
	remove:function() {
		var row = $('#datagrid').datagrid('getSelected');
		if(row==null) {
			top.showInfo('请选择需要删除的制度!');
			return;
		}
		
		$.messager.confirm('信息提示','确定要删除吗？',function(success){
		    if(!success) return;
		    $('#datagrid').datagrid('loading');
		    $.post(contextPath+'/admin/rule/delete/'+row.id,function(result) {
				$('#datagrid').datagrid('loaded');
				if(result.code==0) {
					$('#datagrid').datagrid('reload');
				} else {
					top.showInfo('删除失败!');
				}
			},'json');
		});
	},
	del:function(obj) {
		var id = $(obj).attr('id');
		$.messager.confirm('信息提示','确定要删除吗？',function(success){
		    if(!success) return;
		    $('#datagrid').datagrid('loading');
		    $.post(contextPath+'/admin/rule/delete/'+id,function(result) {
				$('#datagrid').datagrid('loaded');
				if(result.code==0) {
					$('#datagrid').datagrid('reload');
				} else {
					top.showInfo('删除失败!');
				}
			},'json');
		});
	},
	netMsgChange:function(id,maxCount) {
		var inputLength = $('#'+id).val().length;
		if (inputLength == 0) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + maxCount + '个字');
			saveFlag = false;
		} else if (inputLength > 0 && inputLength <= maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，还可以输入' + (maxCount - inputLength) + '个字');
			saveFlag = false;
		} else if (inputLength > maxCount) {
			$('#'+id+'_txt').html(maxCount+'个字以内，已经输入超过<strong class="cred">' + (inputLength - maxCount) + '</strong>个字');
			saveFlag = true;
		}
	}
});